package com.riche.contractbackend.config;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: RedissonConfig
 * @Description: Redisson配置
 * @Author: Gaoruiqi
 * @Date: 2025-09-09 20:28
 * @Version: 1.0
 **/
@Configuration
// 从配置文件读取redis配置
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonConfig {

    // 这两个属性是从application.yml中读取的，必须保持一致
    private String host;
    private String port;

    @Bean
    public RedissonClient redissonClient() {
        // 1. 创建配置
        Config config = new Config();
        String redisAddress = String.format("redis://%s:%s", host, port);
        config.useSingleServer().setAddress(redisAddress).setDatabase(3);
        // 2. 创建实例
        return (RedissonClient) Redisson.create(config);
    }

}
